from sqlalchemy import text
from flask import Blueprint,request
from model.model import *
from datetime import datetime

oupon = Blueprint('/oupon',__name__,url_prefix='/oupon')
@oupon.route('/create',methods=['POST'])
def oupon_creat():
    c_name = request.form.get('c_name')
    price = request.form.get('price')
    end_data = request.form.get('end_time')
    user_id = request.form.get('user_id')
    if not all([c_name,price,user_id,end_data]):
        return {'msg':'前端数据不完整','code':400}
    print(end_data)
    oupon_data = Coupon(c_name=c_name,price=price,user_id=user_id,end_data=end_data)
    db.session.add(oupon_data)
    db.session.commit()
    return '优惠发送成功'

@oupon.route('/coupon_get',methods=['GET'])
def coupon_get():
    id = request.args.get('id')
    user_data = Coupon.query.filter_by(user_id=id).all()
    if not user_data:
        return {'msg':'没有找到这个用户','code':400}
    coupon_list = [{'id':i.id,'c_name':i.c_name,'price':i.price,'start_date':i.start_date,'end_data':i.end_data}for i in user_data]
    return f'这个用户所有的优惠卷是{coupon_list}'

